package com.mera.tektronix.csviewer.domain



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Project)
class ProjectTests {
	
	Department department
	Project project
	
	@Before
	void setUp() {
		mockForConstraintsTests(Project)
		department = new Department(name: "Department", email: "department@mera.ru", phone: "278-88-88#0001")
		project = new Project(name: "Project", email: "project@mera.ru", phone: "278-88-88#0011", department: department)
	}
	
	void testConstraintsNameUnique() {
		Project test = new Project(name: "Project", email: "project@mera.ru", phone: "278-88-88#0011", department: department)
		mockForConstraintsTests(Project, [test])
  
		assertTrue project.validate()
		assertNull project.errors['name']
  
		project.name = "Another project"
		assertTrue project.validate()
		assertNull project.errors['name']
	}
	
	void testConstraintsNameBlank() {
		project.name = ""
		assertFalse project.validate()
		assertEquals 'blank', project.errors['name']

		project.name = "Project"
		assertTrue project.validate()
		assertNull project.errors['name']
	}
	
	void testConstraintsEmailEmail() {
		project.email = 'badEmail'
		assertFalse project.validate()
		assertEquals 'email', project.errors['email']

		project.email = 'mera@mera.ru'
		assertTrue project.validate()
		assertNull project.errors['email']
	}
	
	void testConstraintsPhoneMatches() {
		project.phone = "235678"
		assertFalse project.validate()
		assertEquals 'matches', project.errors['phone']
		
		project.phone = "278-88-65"
		assertTrue project.validate()
		assertNull project.errors['phone']
	}
	
	void testConstraintsDepartmentNullable() {
		project.department = null
		assertFalse project.validate()
		assertEquals 'nullable', project.errors['department']

		project.department = department
		assertTrue project.validate()
		assertNull project.errors['department']
	}
}
